package com.djf.eg.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.djf.eg.entity.SysRole;
import com.djf.eg.entity.UserInfo;
import com.djf.eg.result.Result;
import com.djf.eg.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;

@RestController
@RequestMapping("/users")
public class UserController extends DBaseController {
	
	@Autowired
	UserService userService;
	
	@GetMapping("")
	public Result getUsers(@RequestParam int pageNum, @RequestParam int pageSize) {
		Page<UserInfo> users = userService.getUsers(pageNum, pageSize);
		return Result.success(new PageInfo<UserInfo>(users));
	}
//	
//	@GetMapping("")
//	public Result getUsers() {
//		List<UserInfo> userList = userService.getAllUsers();
//		
//		return Result.success(userList);
//		
//	}
	
	@PostMapping("")
	public Result addUser(@RequestBody UserInfo user) {
		userService.addUser(user);
		return Result.success(user);
	}
	
	@PutMapping("")
	public Result updateUser(@RequestBody UserInfo user) {
		userService.updateUser(user);
		return Result.success(user);
	}
	
	@DeleteMapping("/{userId}")
	public Result deleteUser(@PathVariable("userId") Integer userId) {
		userService.deleteUser(userId);
		return Result.success();
	}
}
